// miniprogram/pages/jx-taolun/jx-taolun.js
const db2=require('../../assets/db')
const { getDate } = require('../../utils/date.js');
const app = getApp();
Page({
  data: {
    allcomments:[],
    hotcomments: [],
    com:''
  },
  video(){
   wx.switchTab({
     url: '../jingxuan/jinxuan',
   })
  },
  // comments:function(e){
  //   console.log(e);
  //   var mes= this.data.allcomments;
  //   var one={};
  //   mes.push(one);
  // },
  bindCommentChange(e) {
    console.log(e, '----------------------');
    this.setData({
      com: e.detail.value
    })
  },
  commentsubmit:function(e){
    let { com } = this.data;
    
    if ( com.trim() == '') {
      wx.showToast({
        title: '请输入内容',
        icon: 'error',
        duration:2000
      });
      return;
    }
    const isLogin = app.globalData.isLogin;
    console.log(isLogin);
    if (!isLogin) {
      wx.showToast({
        title: '请先登录',
        icon: 'error',
        duration:2000//持续时间
      });
      setTimeout(function() {
        wx.navigateTo({
          url: '../login/login'
        })
      }, 2000)
      
      return ;
    }
    const item = {
      id: this.data.allcomments.length + 1,
      collected: 0,
      hpic: app.globalData.userInfo.avatar,
      call: app.globalData.userInfo.username,
      comment: this.data.com,
      date: getDate(),
      num: 0
    };
    this.setData({
      allcomments: [
        ...this.data.allcomments,
        item
      ],
      com: ''
    })
  },
  light:function(e){
    console.log(e);
    var index=e.currentTarget.dataset.index;
    var message= this.data.allcomments;
    for(let i in message){
      if(i == index){
      var collectStatus = false
      if(message[i].collected ==0){
        collectStatus = true
        message[i].collected=parseInt(message[i].collected)+1
        message[i].num=parseInt(message[i].num)+1
      }else{
        collectStatus=false
        message[i].collected = parseInt(message[i].collected)-1
        message[i].num=parseInt(message[i].num)-1
      }
      // wx.showToast({
      //   title: collectStatus,'点赞成功':'取消收藏',
      // })
    }
  }
  this.setData({
    allcomments:message
  })   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let b=options.a;
    console.log(b)
    const allcomments = db2.usersss[b-1].coms;
    const hotcomments = allcomments.filter(item => item.num > 5)
    // console.log(hotcomments, '----------------');
    this.setData({
      allcomments:db2.usersss[b-1].coms,
      hotcomments
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})